package com.app.couponsharelife.ui.main.special
import android.content.Context
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.appbar.AppBarLayout
import com.gyf.barlibrary.ImmersionBar
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.BrandSpecialClassBean
import com.app.couponsharelife.data.bean.HomeClassBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.view.ScaleGeneralTitleView
import com.app.couponsharelife.ui.viewfactory.BrandSpecialFactory
import com.app.couponsharelife.utils.AppUtils
import com.app.couponsharelife.utils.LoadUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_brand_special.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import java.util.HashMap
/**
 *Created by xct on 2019/5/17
 *Description:品牌专场
 */
class BrandSpecialActivity:BaseActivity() {
    private var immersionBar: ImmersionBar?=null
    private var fragmentFactory: BrandSpecialFactory?=null
    private var commonNavigator:CommonNavigator?=null
    private var mCurrentState=0
    private var classList= arrayListOf<HomeClassBean>()
    override val viewLayoutId: Int
        get() = R.layout.activity_brand_special

    override fun initViews() {
        immersionBar= ImmersionBar.with(this)
        immersionBar!!.statusBarDarkFont(false).init()
        ivClose.setOnClickListener { finish() }
        appbarlayout.addOnOffsetChangedListener(object :AppBarLayout.OnOffsetChangedListener{
            override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                if (verticalOffset == 0) {
                    if (mCurrentState!=0){
                        //Log.e("eeee","展开了")
                        immersionBar!!.statusBarDarkFont(false).init()
                        ivClose.setImageResource(R.drawable.ic_back_white)
                        tvTitle.setTextColor(resources.getColor(R.color.white))
                    }
                    mCurrentState=0
                } else if (Math.abs(verticalOffset) >= appBarLayout!!.totalScrollRange) {
                    if (mCurrentState!=2){
                        //Log.e("eeee","折叠了")
                        immersionBar!!.statusBarDarkFont(true).init()
                        ivClose.setImageResource(R.drawable.back_gray)
                        tvTitle.setTextColor(resources.getColor(R.color.black))
                    }
                    mCurrentState=2
                } else {
                    mCurrentState=1
                }
            }
        })
        getData()
    }
    private fun setViewPager(){
        fragmentFactory=BrandSpecialFactory()
        viewpager.offscreenPageLimit=6
        viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getItem(position: Int): Fragment {
                return fragmentFactory!!.createFragment(position,classList[position])
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return classList[position].name
            }
        }
        commonNavigator= CommonNavigator(this)
        commonNavigator!!.adapter=object : CommonNavigatorAdapter(){
            override fun getTitleView(p0: Context, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView= ScaleGeneralTitleView(p0)
                colorTransitionPagerTitleView.normalColor = resources.getColor(R.color.gray_66)
                colorTransitionPagerTitleView.textSize=16f
                colorTransitionPagerTitleView.selectedColor = resources.getColor(R.color.red_ff038)
                colorTransitionPagerTitleView.text = classList[p1].name
                colorTransitionPagerTitleView.setOnClickListener { viewpager.currentItem = p1 }
                return colorTransitionPagerTitleView
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getIndicator(p0: Context?): IPagerIndicator? {
                val indicator= LinePagerIndicator(p0)
                indicator.setColors(resources.getColor(R.color.red_ff038))
                indicator.mode= LinePagerIndicator.MODE_EXACTLY
                indicator.lineWidth= AppUtils.dip2px(this@BrandSpecialActivity,20f).toFloat()
                indicator.lineHeight= AppUtils.dip2px(this@BrandSpecialActivity,2f).toFloat()
                return indicator
            }
        }
        commonNavigator!!.onFocusChangeListener=object : View.OnFocusChangeListener{
            override fun onFocusChange(v: View?, hasFocus: Boolean) {

            }
        }
        tablayout.navigator=commonNavigator
        ViewPagerHelper.bind(tablayout, viewpager)
    }
    private fun getData(){
        showProgressDialog("加载中...")
        val map= HashMap<String,String>()
        RetrofitWorker.retrofitWorker.getBrandClassfy(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<BrandSpecialClassBean>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<BrandSpecialClassBean>) {
                        dismissProgressDialog()
                        if (it.code==200){
                            if (it.data!=null&&it.data!!.brandClassfy!=null&&it.data!!.brandClassfy!!.isNotEmpty()){
                                classList.clear()
                                classList.addAll(it.data!!.brandClassfy!!)
                                val imagUrl=Contacts.NEWREMOTE_IMAGE_URL +it.data!!.images?:""
                                LoadUtils().loadCommonImag(this@BrandSpecialActivity,ivTopBanner,imagUrl,R.drawable.ic_app_place_holder3)
                                setViewPager()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }
    override fun onDestroy() {
        super.onDestroy()
        immersionBar!!.destroy()
    }
}